package com.hushan.elevator.service.impl;

import com.hushan.elevator.dao.RiskInfoRepository;
import com.hushan.elevator.model.RiskInfo;
import com.hushan.elevator.service.RiskInfoService;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: zzy
 * @Date: 2024/10/23 0:04
 * @Title:
 * @Version:
 * @Package: com.hushan.elevator.service.impl
 */
@Service
public class RiskInfoServiceImpl implements RiskInfoService {

    private final RiskInfoRepository riskInfoRepository;

    @Autowired
    public RiskInfoServiceImpl(RiskInfoRepository riskInfoRepository) {
        this.riskInfoRepository = riskInfoRepository;
    }

    @Override
    public List<RiskInfo> findAllRiskInfos() {
        RiskInfo riskInfo = new RiskInfo();
        riskInfo.setIsDeleted(false);
        Example<RiskInfo> example = Example.of(riskInfo);
        return riskInfoRepository.findAll(example);
    }

    @Override
    public RiskInfo findRiskInfoById(Integer id) {
        return riskInfoRepository.findById(id).orElse(null);
    }

    @Override
    public RiskInfo findBySerialNumber(String serialNumber) {
        return riskInfoRepository.findBySerialNumber(serialNumber);
    }

    @Override
    public List<RiskInfo> findByIsDeleted(Boolean isDeleted) {
        return List.of();
    }


    @Override
    public void addRiskInfo(RiskInfo riskInfo) {
        riskInfoRepository.save(riskInfo);
    }

    @Override
    public void updateRiskInfo(RiskInfo riskInfo) throws Exception {
        RiskInfo info = findRiskInfoById(riskInfo.getRiskId());
        if (info != null) {
            riskInfoRepository.save(riskInfo);
        }else{
            throw new Exception();
        }
    }


    @Override
    @Transactional
    public void deleteRiskInfo(Integer id) throws Exception {
        RiskInfo info = findRiskInfoById(id);
        if (info != null) {
            riskInfoRepository.deleteById(id);
        }else{
            throw new Exception();
        }
    }
}